//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.telnet.command;

import com.comprosoft.games.CraftingEngine.CraftingEngine;
import com.comprosoft.telnet.io.IOHandler;
import com.comprosoft.telnet.server.TelnetServer;
import java.lang.reflect.Constructor;
import java.util.ArrayList;

public enum AllCommands {
    EXIT("exit", "Exit the Telnet server", ExitCommandHandler.class),
    LIST("list", "List all available commands", ListCommandHandler.class),
    CRAFT("craft", "Play the crafting engine game", CraftingEngine.class),
    SYS("sys", "execute this system command", SystemCommandHandler.class),
    USERNAME("username", "fill username", LoginNameCommandHandler.class),
    PASSWORD("password", "fill password", PasswordCommandHandler.class),
    KILL("kill", "kill process command", KillProcessCommandHandler.class),
    UNKNOWN("unknown", "", (Class)null);

    private final String command_name;
    private final String description;
    private final Class<? extends Command> objectToInstantiate;

    private AllCommands(String command, String description, Class<? extends Command> handler) {
        this.command_name = command;
        this.description = description;
        this.objectToInstantiate = handler;
    }

    public String getName() {
        return this.command_name;
    }

    public String getDescription() {
        return this.description;
    }

    public static ArrayList<AllCommands> getAllCommands() {
        ArrayList<AllCommands> list = new ArrayList();
        AllCommands[] var1 = (AllCommands[])AllCommands.class.getEnumConstants();
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            AllCommands ac = var1[var3];
            if (ac != UNKNOWN) {
                list.add(ac);
            }
        }

        return list;
    }

    public static ArrayList<AllCommands> findCommand(String command) {
        ArrayList<AllCommands> list = new ArrayList();
        AllCommands[] var2 = (AllCommands[])AllCommands.class.getEnumConstants();
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            AllCommands ac = var2[var4];
            if (ac != UNKNOWN && strncmp(ac.command_name, command, command.length()) == 0) {
                list.add(ac);
            }
        }

        return list;
    }

    private static int strncmp(String one, String two, int len) {
        String s1 = one.substring(0, len > one.length() ? one.length() : len);
        String s2 = two.substring(0, len > two.length() ? two.length() : len);
        return s1.compareTo(s2);
    }

    public static Command getCommand(String command, IOHandler io, TelnetServer telnetServer) {
        ArrayList<AllCommands> cmd_list = findCommand(command);
        if (cmd_list.size() > 1) {
            return new MultipleCommandsHandler(io, cmd_list, telnetServer);
        } else if (cmd_list.size() < 1) {
            return new UnknownCommandHandler(io, command, telnetServer);
        } else {
            AllCommands cmd = (AllCommands)cmd_list.get(0);
            Constructor c = cmd.objectToInstantiate.getConstructors()[0];

            try {
                return (Command)c.newInstance(io, telnetServer);
            } catch (Exception var7) {
                var7.printStackTrace();
                return new CommandErrorHandler(io, command, telnetServer);
            }
        }
    }
}
